package com.biovr.backend.security;




import java.util.ArrayList;
import java.util.List;
import com.biovr.backend.domain.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
public final class JwtUserFactory {

    private JwtUserFactory() {
    }

    public static JwtUser create(User user) {
        return new JwtUser(
                user.getId(),
                user.getUsername(),
                user.getPassword(),
                user.getRealName(),
                user.getGender(),
                user.getAvater(),
                user.getPhone(),
                user.getEmail(),
                user.getRegisterDate(),
                user.getLastLogin(),
                user.getLastLoginIp(),
                user.getLoginTimes(),
                user.getSchoolId(),
                user.getSchoolName(),
                user.getStuMajor(),
                user.getStuGrade(),
                user.getStuClass(),
                user.getSchoolNum(),
                user.getTeacherDepartment(),
                user.getLastPasswordResetDate(),
                mapToGrantedAuthorities(user.getRoles())
        );
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(List<String> authorities) {
        List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
    for(String a : authorities){
        SimpleGrantedAuthority authority = new SimpleGrantedAuthority(a);
        list.add(authority);
    }
        return list;
    }
}

